# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import sys
import shutil


# def unzip(app):
#     print("开始通过apktool 解压apk")
#     apps = app.split(".")[0]
#     os.system(f"apktool_2.4.1.jar d -f {app} -o {apps}")
#     return apps

def unzip(file_path):
    print("解压apk文件")
    extract_path = file_path.split(".")[0]
    print(extract_path)

    shutil.unpack_archive(file_path, extract_path, 'zip')
    return extract_path
def loopdir(pathName):
    print("开始将dex文件转成jar",pathName)
    for root,dirs,files in os.walk(pathName):
        for f in files:
            if f.endswith("dex"):
                absoluteName = f"{root}/{f}"
                saveName = absoluteName.split(".")[0]+".jar"
                cmd = f"dex2jar\\d2j-dex2jar.bat {absoluteName}  --output {saveName}"
                print(cmd)
                os.system(cmd)
                toJava(saveName)
        break

def toJava(name):
    saveName = name.split(".")[0]
    print("开始将jar文件转成java",saveName)
    if not os.path.exists(saveName) :
        os.makedirs(saveName) 
    cmd = f"java -jar cfr.jar {name}  --outputdir {saveName}"
    print(cmd)
    os.system(cmd)
# d2j-dex2jar 

def function(name):
    apps = unzip(name)
    loopdir(apps)

if __name__ == '__main__':
    function(r"J:\log\主题商店_10.0.apk")
